package com.tzy.gateway.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 用代码配置网关
 */
@Configuration
public class RouteConfig {
    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder) {
//        return routeLocatorBuilder.routes()
//                .route("test3",r-> r.path("/s")
//                        .uri("https://www.baidu.com/s")).build();
        return routeLocatorBuilder.routes()
                .route("test3", r -> r.path("/s/**")
                        .uri("https://www.baidu.com/s"))
                .route("test4", r -> r.path("/gitee")
                        .uri("https://gitee.com")
                )
//                .route("lb", r -> r.path("/order/**")
//                        .filters(f -> f.addRequestHeader("uname", "64556asldfk"))
//                        .uri("lb://order"))
//                .route("lb",r-> r.path("/dishes/**")
//                        .uri("lb://dishes"))
                .route("dishes",r-> r.path("/dishes/**")
                        .uri("lb://dishes"))
                .route("evaluation",r-> r.path("/evaluation/**")
                        .uri("lb://evaluation"))
                .route("user",r-> r.path("/user/**")
                        .uri("lb://USER"))
                .route("shop",r-> r.path("/shop/**")
                        .uri("lb://shop"))
                .route("footprint",r-> r.path("/footprint/**")
                .uri("lb://footprint"))
                .route("collection",r-> r.path("/collection/**")
                        .uri("lb://collection"))
                .route("shopcascoupon",r-> r.path("/shopcascoupon/**")
                        .uri("lb://shopcascoupon"))
                .route("orders",r-> r.path("/orders/**")
                        .uri("lb://orders"))
                .route("shopping-cart",r-> r.path("/shopping-cart/**")
                        .uri("lb://shopping-cart"))
                .route("assets",r-> r.path("/assets/**")
                        .uri("lb://assets"))
                .build();
    }

    public RouteLocator routeLocatorTest(RouteLocatorBuilder builder) {
        return builder.routes().route("test3", r -> {
            return r.path("/s").uri("https://www.baidu.com");
        })
                .route("test4", r -> {
                    return r.path("/map").uri("https://news.sina.com.cn");
                })
                .route("lb", r -> {
                    return r.path("/order/**")
                            .filters(f -> {
                                return f.addRequestHeader("uname", "abcde");
                            })
                            .uri("lb://order");
                })
                .build();
    }
}
